#!/bin/sh
#
# Start/stop/restart GNU social queuedaemon.
#
# Copyright (C) 2024  Ricardo García Jiménez <ricardogj08@riseup.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#

gnusocial_queuedaemon_start() {
  if [ -r /var/lib/gnusocial/run/queuedaemon.generic.pid ]; then
    echo "GNU social queuedaemon is already running: $(cat /var/lib/gnusocial/run/queuedaemon.generic.pid)"
    exit 1
  fi

  if [ -r /var/lib/gnusocial/scripts/startdaemons.sh ]; then
    sh /var/lib/gnusocial/scripts/startdaemons.sh
    echo "Starting GNU social queuedaemon: sh /var/lib/gnusocial/scripts/startdaemons.sh"
  fi
}

gnusocial_queuedaemon_error() {
  echo "GNU social queuedaemon does not seem to be running"
  exit 1
}

gnusocial_queuedaemon_stop() {
  if [ -r /var/lib/gnusocial/run/queuedaemon.generic.pid ] && [ -r /var/lib/gnusocial/scripts/stopdaemons.sh ]; then
    sh /var/lib/gnusocial/scripts/stopdaemons.sh
  else
    gnusocial_queuedaemon_error
  fi
}

gnusocial_queuedaemon_restart() {
  gnusocial_queuedaemon_stop
  gnusocial_queuedaemon_start
}

gnusocial_queuedaemon_status() {
  if [ -r /var/lib/gnusocial/run/queuedaemon.generic.pid ]; then
    echo "GNU social queuedaemon is running: $(cat /var/lib/gnusocial/run/queuedaemon.generic.pid)"
  else
    gnusocial_queuedaemon_error
  fi
}

case "$1" in
  'start')
    gnusocial_queuedaemon_start
    ;;
  'stop')
    gnusocial_queuedaemon_stop
    ;;
  'restart')
    gnusocial_queuedaemon_restart
    ;;
  'status')
    gnusocial_queuedaemon_status
    ;;
  *)
    echo "usage $0 start|stop|restart|status"
esac
